/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.db.table;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Hashtable;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

import com.edi.Constant;
import com.edi.db.EdiDb;
import com.edi.exceptions.EdiException;

public class RowEntity {
	private static final Log log = LogFactory.getLog(RowEntity.class);
	private Hashtable<String, ColumnCell> columnCells;

	public RowEntity() {
		columnCells = new Hashtable<String, ColumnCell>();
	}

	public RowEntity(TableEntity tableEntity) {
		columnCells = new Hashtable<String, ColumnCell>();
		for (FieldEntity field : tableEntity.getFieldEntitys().values()) {
			ColumnCell cell = new ColumnCell();
			cell.setFieldEntity(field);
			cell.setFieldType(field.getFieldType());
			cell.setFieldName(field.getFieldName().toLowerCase());
			cell.setFieldValue(null);
			columnCells.put(field.getFieldName().toLowerCase(), cell);
		}
	}

	public Hashtable<String, ColumnCell> getColumnCells() {
		return columnCells;
	}

	public void addCell(String fieldName, Object value) {
		if (value instanceof ColumnCell) {
			ColumnCell cell = getCell(fieldName.toLowerCase());
			if (cell != null)
				cell = (ColumnCell) value;
			else
				columnCells.put(fieldName, (ColumnCell) value);

			//			columnCells.put(fieldName, (ColumnCell) value);
		} else {
			setCell(fieldName, value);
		}
	}

	/**
	 * 从xml中解析并添加数据信息
	 * 
	 * @param element
	 *            格式为 <fieldname fieldType="fieldTye" primaryKey><![CDATA[张三]]></fieldname>
	 */
	public void addCell(Element cellE) throws EdiException {
		String fieldName = cellE.getName();
		String fieldValue = cellE.getText();
		String fieldType = cellE.attributeValue("fieldType");

		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		if (cell == null) {
			throw new EdiException("未找到字段：" + fieldName);
		}

		//		cell.setFieldName(fieldName.toLowerCase());
		//		FieldEntity field = tableEntity.getFieldEntitys().get(
		//				cell.getFieldName());
		//		cell.setFieldEntity(field);
		//		cell.setFieldType(field.getFieldType());
		if (fieldValue == null)
			cell.setFieldValue(null);
		else {
			if (StringUtils.equalsIgnoreCase(fieldType, EdiDb.TYPE_BOOLEAN)) {
				cell.setFieldValue(StringUtils.equalsIgnoreCase(fieldValue,
						"true"));
			} else if (StringUtils.equalsIgnoreCase(fieldType,
					EdiDb.TYPE_CURRENCY)) {
				cell.setFieldValue(new BigDecimal(fieldValue));
			} else if (StringUtils.equalsIgnoreCase(fieldType, EdiDb.TYPE_DATE)) {
				try {
					cell.setFieldValue(DateUtils.parseDate(fieldValue,
							Constant.simpleDateFormate));
				} catch (Exception e) {
					e.printStackTrace();
					log.error(e.getMessage());
				}
			} else if (StringUtils.equalsIgnoreCase(fieldType,
					EdiDb.TYPE_DECIMAL)) {
				cell.setFieldValue(new BigDecimal(fieldValue));
			} else if (StringUtils
					.equalsIgnoreCase(fieldType, EdiDb.TYPE_FLOAT)) {
				cell.setFieldValue(new Float(fieldValue).floatValue());
			} else if (StringUtils.equalsIgnoreCase(fieldType, EdiDb.TYPE_INT)) {
				cell.setFieldValue(new Integer(fieldValue).intValue());
			} else {
				cell.setFieldValue(fieldValue);
			}
		}
		columnCells.put(fieldName.toLowerCase(), cell);
	}

	public String getString(String fieldName) {
		//		log.debug(" fieldName ="+fieldName);
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		//		log.debug(" cell ="+cell);
		return cell == null ? null : cell.getFieldValue() == null ? "" : cell
				.getFieldValue().toString();
	}

	public void setCell(String fieldName, Object fieldValue) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		if (cell != null)
			cell.setFieldValue(fieldValue);
	}

	public int getInt(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		return (cell == null || cell.getFieldValue() == null) ? 0 : Integer
				.valueOf(cell.getFieldValue().toString());
	}

	public float getFloat(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		return (cell == null || cell.getFieldValue() == null) ? 0f : Float
				.valueOf(cell.getFieldValue().toString());
	}

	public BigDecimal getBigDecimal(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		return (cell == null || cell.getFieldValue() == null) ? new BigDecimal(
				0) : ((BigDecimal) cell.getFieldValue());
	}

	public Date getDate(String fieldName) {
		Date date = null;
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		if (cell == null)
			return null;
		else {
			if (cell.getFieldValue() != null)
				date = (java.util.Date) cell.getFieldValue();
		}
		return date;
	}

	public Timestamp getTimeStamp(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		return cell == null ? null : cell.getFieldValue() == null ? null
				: (Timestamp) cell.getFieldValue();
	}

	public Object getValue(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		return cell == null ? null : cell.getFieldValue();
	}

	public Boolean getBoolean(String fieldName) {
		ColumnCell cell = columnCells.get(fieldName.toLowerCase());
		//log.debug(" cell == null " + (cell == null));
		//		if (cell != null)
		//			log.debug(" cell.getFieldValue()==null "
		//					+ (cell.getFieldValue() == null));
		if (cell == null || cell.getFieldValue() == null)
			return null;
		Object value = cell.getFieldValue();
		//System.out.println("目前获得boolean值是："+value);
		if (value instanceof String)
			if ("true".equalsIgnoreCase((String)value) || "1".equals(value))
				return true;
			else
				return false;
		if (value instanceof Integer)
			if ((Integer)value == 0)
				return false;
			else
				return true;
		return false;
	}

	public ColumnCell getCell(String fieldName) {
		return columnCells.get(fieldName.toLowerCase());
	}

}
